home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / diff_2_1.lha / diff-2.1 / util.c < prev    next >
C/C++ Source or Header  |  1993-02-04  |  18KB  |  778 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. #ifdef AMIGA
  23. #include <signal.h>
  24. #include <exec/types.h>
  25. #include <dos/dostags.h>
  26. #include <proto/dos.h>
  27. #include <proto/exec.h>
  28.  
  29. extern struct DosLibrary *DOSBase;
  30. static char tmpfilename[L_tmpnam];
  31. #endif /* AMIGA */
  32.  
  33. /* Use when a system call returns non-zero status.
  34.    TEXT should normally be the file name.  */
  35.  
  36. void
  37. perror_with_name (text)
  38.      char *text;
  39. {
  40.   int e = errno;
  41.   fprintf (stderr, "%s: ", program);
  42.   errno = e;
  43.   perror (text);
  44. }
  45.  
  46. /* Use when a system call returns non-zero status and that is fatal.  */
  47.  
  48. void
  49. pfatal_with_name (text)
  50.      char *text;
  51. {
  52.   int e = errno;
  53.   print_message_queue ();
  54.   fprintf (stderr, "%s: ", program);
  55.   errno = e;
  56.   perror (text);
  57.   exit (2);
  58. }
  59.  
  60. /* Print an error message from the format-string FORMAT
  61.    with args ARG1 and ARG2.  */
  62.  
  63. void
  64. error (format, arg, arg1)
  65.      char *format;
  66.      char *arg;
  67.      char *arg1;
  68. {
  69.   fprintf (stderr, "%s: ", program);
  70.   fprintf (stderr, format, arg, arg1);
  71.   fprintf (stderr, "\n");
  72. }
  73.  
  74. /* Print an error message containing the string TEXT, then exit.  */
  75.  
  76. void
  77. fatal (m)
  78.      char *m;
  79. {
  80.   print_message_queue ();
  81.   error ("%s", m, 0);
  82.   exit (2);
  83. }
  84.  
  85. /* Like printf, except if -l in effect then save the message and print later.
  86.    This is used for things like "binary files differ" and "Only in ...".  */
  87.  
  88. void
  89. message (format, arg1, arg2)
  90.      char *format, *arg1, *arg2;
  91. {
  92.   if (paginate_flag)
  93.     {
  94.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  95.       if (msg_chain_end == 0)
  96.     msg_chain = msg_chain_end = new;
  97.       else
  98.     {
  99.       msg_chain_end->next = new;
  100.       msg_chain_end = new;
  101.     }
  102.       new->format = format;
  103.       new->arg1 = concat (arg1, "", "");
  104.       new->arg2 = concat (arg2, "", "");
  105.       new->next = 0;
  106.     }
  107.   else
  108.     {
  109.       if (sdiff_help_sdiff)
  110.     putchar (' ');
  111.       printf (format, arg1, arg2);
  112.     }
  113. }
  114.  
  115. /* Output all the messages that were saved up by calls to `message'.  */
  116.  
  117. void
  118. print_message_queue ()
  119. {
  120.   struct msg *m;
  121.  
  122.   for (m = msg_chain; m; m = m->next)
  123.     printf (m->format, m->arg1, m->arg2);
  124. }
  125.  
  126. /* Call before outputting the results of comparing files NAME0 and NAME1
  127.    to set up OUTFILE, the stdio stream for the output to go to.
  128.  
  129.    Usually, OUTFILE is just stdout.  But when -l was specified
  130.    we fork off a `pr' and make OUTFILE a pipe to it.
  131.    `pr' then outputs to our stdout.  */
  132.  
  133. static char *current_name0;
  134. static char *current_name1;
  135. static int current_depth;
  136.  
  137. void
  138. setup_output (name0, name1, depth)
  139.      char *name0, *name1;
  140.      int depth;
  141. {
  142.   current_name0 = name0;
  143.   current_name1 = name1;
  144.   current_depth = depth;
  145.   outfile = 0;
  146. }
  147.  
  148. void
  149. begin_output ()
  150. {
  151.   char *name;
  152.  
  153.   if (outfile != 0)
  154.     return;
  155.  
  156.   /* Construct the header of this piece of diff.  */
  157.   name = (char *) xmalloc (strlen (current_name0) + strlen (current_name1)
  158.                + strlen (switch_string) + 15);
  159.  
  160.   strcpy (name, "diff");
  161.   strcat (name, switch_string);
  162.   strcat (name, " ");
  163.   strcat (name, current_name0);
  164.   strcat (name, " ");
  165.   strcat (name, current_name1);
  166.  
  167.   if (paginate_flag)
  168. #ifndef AMIGA
  169.     {
  170.       int pipes[2];
  171.       int desc;
  172.  
  173.       /* For a `pr' and make OUTFILE a pipe to it.  */
  174.       if (pipe (pipes) < 0)
  175.     pfatal_with_name ("pipe");
  176.  
  177.       fflush (stdout);
  178.  
  179.       desc = vfork ();
  180.       if (desc < 0)
  181.     pfatal_with_name ("vfork");
  182.  
  183.       if (desc == 0)
  184.     {
  185.       close (pipes[1]);
  186.       if (pipes[0] != fileno (stdin))
  187.         {
  188.           if (dup2 (pipes[0], fileno (stdin)) < 0)
  189.         pfatal_with_name ("dup2");
  190.           close (pipes[0]);
  191.         }
  192.  
  193.       if (execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0) < 0)
  194.         pfatal_with_name (PR_FILE_NAME);
  195.     }
  196.       else
  197.     {
  198.       close (pipes[0]);
  199.       outfile = fdopen (pipes[1], "w");
  200.     } 
  201.     }
  202. #else /* AMIGA */
  203.     {
  204.       if (DOSBase->dl_lib.lib_Version < 37)
  205.     {
  206.       fputs ("Need Amiga OS 2.0 (V.37) to paginate. ", stderr);
  207.       fputs ("Using stdout instead.\n", stderr);
  208.       outfile = stdout;
  209.     }
  210.       else
  211.     {
  212.       /* Output the differences to a file and process the file with
  213.        * pr later on. This is a really bad hack (a kluge??? :-) ),
  214.        * but I couldn't find something better... */
  215.       tmpnam (tmpfilename);
  216.       outfile = fopen (tmpfilename, "w");
  217.       if (outfile == NULL)
  218.         pfatal_with_name ("pipe");
  219.     }
  220.     }
  221. #endif
  222.   else
  223.     {
  224.  
  225.       /* If -l was not specified, output the diff straight to `stdout'.  */
  226.  
  227.       outfile = stdout;
  228.  
  229.       /* If handling multiple files (because scanning a directory),
  230.      print which files the following output is about.  */
  231.       if (current_depth > 0)
  232.     printf ("%s\n", name);
  233.     }
  234.  
  235.   free (name);
  236.  
  237.   /* A special header is needed at the beginning of context output.  */
  238.   switch (output_style)
  239.     {
  240.     case OUTPUT_CONTEXT:
  241.       print_context_header (files, 0);
  242.       break;
  243.  
  244.     case OUTPUT_UNIFIED:
  245.       print_context_header (files, 1);
  246.       break;
  247.  
  248.     default:
  249.       break;
  250.     }
  251. }
  252.  
  253. /* Call after the end of output of diffs for one file.
  254.    Close OUTFILE and get rid of the `pr' subfork.  */
  255.  
  256. void
  257. finish_output ()
  258. {
  259.   if (outfile != 0 && outfile != stdout)
  260. #ifndef AMIGA
  261.     {
  262.       fclose (outfile);
  263.       wait (0);
  264.     }
  265. #else /* AMIGA */
  266.     {
  267.       char *command_line;
  268.       struct TagItem STags[5];
  269.       BPTR StdInPr;
  270.  
  271.       /* Close the temporary file and run pr with this file as input */
  272.       fclose (outfile);
  273.       command_line = (char *) xmalloc (strlen (current_name0)
  274.                        + strlen (current_name1)
  275.                        + strlen (switch_string) + 30);
  276.       strcpy (command_line, "pr -f -h \"");
  277.       strcat (command_line, "diff");
  278.       strcat (command_line, switch_string);
  279.       strcat (command_line, " ");
  280.       strcat (command_line, current_name0);
  281.       strcat (command_line, " ");
  282.       strcat (command_line, current_name1);
  283.       strcat (command_line, "\"");
  284.       StdInPr = Open (tmpfilename, MODE_OLDFILE);
  285.       if (StdInPr == NULL)
  286.     pfatal_with_name ("pipe");
  287.       STags[0].ti_Tag = SYS_Input;
  288.       STags[0].ti_Data = StdInPr;
  289.       STags[1].ti_Tag = SYS_Output;
  290.       STags[1].ti_Data = Output ();
  291.       STags[2].ti_Tag = SYS_Asynch;
  292.       STags[2].ti_Data = FALSE;
  293.       STags[3].ti_Tag = SYS_UserShell;
  294.       STags[3].ti_Data = TRUE;
  295.       STags[4].ti_Tag = TAG_DONE;
  296.       if (System (command_line, STags) != 0)
  297.     {
  298.       Close (StdInPr);
  299.       DeleteFile (tmpfilename);
  300.       pfatal_with_name ("pr");
  301.     }
  302.       Close (StdInPr);
  303.       DeleteFile (tmpfilename);
  304.     }
  305. #endif /* !AMIGA */
  306.  
  307.   outfile = 0;
  308. }
  309.  
  310. /* Compare two lines (typically one from each input file)
  311.    according to the command line options.
  312.    Return 1 if the lines differ, like `bcmp'.  */
  313.  
  314. int
  315. line_cmp (s1, len1, s2, len2)
  316.      const char *s1, *s2;
  317.      int len1, len2;
  318. {
  319.   register const unsigned char *t1, *t2;
  320.   register unsigned char end_char = line_end_char;
  321.  
  322.   /* Check first for exact identity.
  323.      If that is true, return 0 immediately.
  324.      This detects the common case of exact identity
  325.      faster than complete comparison would.  */
  326.  
  327.   if (len1 == len2 && bcmp (s1, s2, len1) == 0)
  328.     return 0;
  329.  
  330.   /* Not exactly identical, but perhaps they match anyway
  331.      when case or whitespace is ignored.  */
  332.  
  333.   if (ignore_case_flag || ignore_space_change_flag || ignore_all_space_flag)
  334.     {
  335.       t1 = (const unsigned char *) s1;
  336.       t2 = (const unsigned char *) s2;
  337.  
  338.       while (1)
  339.     {
  340.       register unsigned char c1 = *t1++;
  341.       register unsigned char c2 = *t2++;
  342.  
  343.       /* Ignore horizontal whitespace if -b or -w is specified.  */
  344.  
  345.       if (ignore_all_space_flag)
  346.         {
  347.           /* For -w, just skip past any white space.  */
  348.           while (Is_space (c1)) c1 = *t1++;
  349.           while (Is_space (c2)) c2 = *t2++;
  350.         }
  351.       else if (ignore_space_change_flag)
  352.         {
  353.           /* For -b, advance past any sequence of whitespace in line 1
  354.          and consider it just one Space, or nothing at all
  355.          if it is at the end of the line.  */
  356.           if (c1 == ' ' || c1 == '\t')
  357.         {
  358.           while (1)
  359.             {
  360.               c1 = *t1++;
  361.               if (c1 == end_char)
  362.             break;
  363.               if (c1 != ' ' && c1 != '\t')
  364.             {
  365.               --t1;
  366.               c1 = ' ';
  367.               break;
  368.             }
  369.             }
  370.         }
  371.  
  372.           /* Likewise for line 2.  */
  373.           if (c2 == ' ' || c2 == '\t')
  374.         {
  375.           while (1)
  376.             {
  377.               c2 = *t2++;
  378.               if (c2 == end_char)
  379.             break;
  380.               if (c2 != ' ' && c2 != '\t')
  381.             {
  382.               --t2;
  383.               c2 = ' ';
  384.               break;
  385.             }
  386.             }
  387.         }
  388.         }
  389.  
  390.       /* Upcase all letters if -i is specified.  */
  391.  
  392.       if (ignore_case_flag)
  393.         {
  394.           if (islower (c1))
  395.         c1 = toupper (c1);
  396.           if (islower (c2))
  397.         c2 = toupper (c2);
  398.         }
  399.  
  400.       if (c1 != c2)
  401.         break;
  402.       if (c1 == end_char)
  403.         return 0;
  404.     }
  405.     }
  406.  
  407.   return (1);
  408. }
  409.  
  410. /* Find the consecutive changes at the start of the script START.
  411.    Return the last link before the first gap.  */
  412.  
  413. struct change *
  414. find_change (start)
  415.      struct change *start;
  416. {
  417.   return start;
  418. }
  419.  
  420. struct change *
  421. find_reverse_change (start)
  422.      struct change *start;
  423. {
  424.   return start;
  425. }
  426.  
  427. /* Divide SCRIPT into pieces by calling HUNKFUN and
  428.    print each piece with PRINTFUN.
  429.    Both functions take one arg, an edit script.
  430.  
  431.    HUNKFUN is called with the tail of the script
  432.    and returns the last link that belongs together with the start
  433.    of the tail.
  434.  
  435.    PRINTFUN takes a subscript which belongs together (with a null
  436.    link at the end) and prints it.  */
  437.  
  438. void
  439. print_script (script, hunkfun, printfun)
  440.      struct change *script;
  441.      struct change * (*hunkfun) ();
  442.      void (*printfun) ();
  443. {
  444.   struct change *next = script;
  445.  
  446.   while (next)
  447.     {
  448.       struct change *this, *end;
  449.  
  450.       /* Find a set of changes that belong together.  */
  451.       this = next;
  452.       end = (*hunkfun) (next);
  453.  
  454.       /* Disconnect them from the rest of the changes,
  455.      making them a hunk, and remember the rest for next iteration.  */
  456.       next = end->link;
  457.       end->link = NULL;
  458. #ifdef DEBUG
  459.       debug_script (this);
  460. #endif
  461.  
  462.       /* Print this hunk.  */
  463.       (*printfun) (this);
  464.  
  465.       /* Reconnect the script so it will all be freed properly.  */
  466.       end->link = next;
  467.     }
  468. }
  469.  
  470. /* Print the text of a single line LINE,
  471.    flagging it with the characters in LINE_FLAG (which say whether
  472.    the line is inserted, deleted, changed, etc.).  */
  473.  
  474. void
  475. print_1_line (line_flag, line)
  476.      const char *line_flag;
  477.      const char * const *line;
  478. {
  479.   const char *text = line[0], *limit = line[1]; /* Help the compiler.  */
  480.   FILE *out = outfile; /* Help the compiler some more.  */
  481.   const char *flag_format = 0;
  482.  
  483.   /* If -T was specified, use a Tab between the line-flag and the text.
  484.      Otherwise use a Space (as Unix diff does).
  485.      Print neither space nor tab if line-flags are empty.  */
  486.  
  487.   if (line_flag != NULL && line_flag[0] != 0)
  488.     {
  489.       flag_format = tab_align_flag ? "%s\t" : "%s ";
  490.       fprintf (out, flag_format, line_flag);
  491.     }
  492.  
  493.   output_1_line (text, limit, flag_format, line_flag);
  494.  
  495.   if ((line_flag == NULL || line_flag[0] != 0) && limit[-1] != '\n'
  496.       && line_end_char == '\n')
  497.     fprintf (out, "\n\\ No newline at end of file\n");
  498. }
  499.  
  500. /* Output a line from TEXT up to LIMIT.  Without -t, output verbatim.
  501.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  502.    is nonzero, output it with argument LINE_FLAG after every
  503.    internal carriage return, so that tab stops continue to line up.  */
  504.  
  505. void
  506. output_1_line (text, limit, flag_format, line_flag)
  507.      const char *text, *limit, *flag_format, *line_flag;
  508. {
  509.   if (!tab_expand_flag)
  510.     fwrite (text, sizeof (char), limit - text, outfile);
  511.   else
  512.     {
  513.       register FILE *out = outfile;
  514.       register char c;
  515.       register const char *t = text;
  516.       register unsigned column = 0;
  517.  
  518.       while (t < limit)
  519.     switch ((c = *t++))
  520.       {
  521.       case '\t':
  522.         {
  523.           unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
  524.           column += spaces;
  525.           do
  526.         putc (' ', out);
  527.           while (--spaces);
  528.         }
  529.         break;
  530.  
  531.       case '\r':
  532.         putc (c, out);
  533.         if (flag_format && t < limit && *t != '\n')
  534.           fprintf (out, flag_format, line_flag);
  535.         column = 0;
  536.         break;
  537.  
  538.       case '\b':
  539.         if (column == 0)
  540.           continue;
  541.         column--;
  542.         putc (c, out);
  543.         break;
  544.  
  545.       default:
  546.         if (textchar[(unsigned char) c])
  547.           column++;
  548.         /* fall into */
  549.       case '\f':
  550.       case '\v':
  551.         putc (c, out);
  552.         break;
  553.       }
  554.     }
  555. }
  556.  
  557. int
  558. change_letter (inserts, deletes)
  559.      int inserts, deletes;
  560. {
  561.   if (!inserts)
  562.     return 'd';
  563.   else if (!deletes)
  564.     return 'a';
  565.   else
  566.     return 'c';
  567. }
  568.  
  569. /* Translate an internal line number (an index into diff's table of lines)
  570.    into an actual line number in the input file.
  571.    The internal line number is LNUM.  FILE points to the data on the file.
  572.  
  573.    Internal line numbers count from 0 starting after the prefix.
  574.    Actual line numbers count from 1 within the entire file.  */
  575.  
  576. int
  577. translate_line_number (file, lnum)
  578.      struct file_data *file;
  579.      int lnum;
  580. {
  581.   return lnum + file->prefix_lines + 1;
  582. }
  583.  
  584. void
  585. translate_range (file, a, b, aptr, bptr)
  586.      struct file_data *file;
  587.      int a, b;
  588.      int *aptr, *bptr;
  589. {
  590.   *aptr = translate_line_number (file, a - 1) + 1;
  591.   *bptr = translate_line_number (file, b + 1) - 1;
  592. }
  593.  
  594. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  595.    If the two numbers are identical, print just one number.
  596.  
  597.    Args A and B are internal line numbers.
  598.    We print the translated (real) line numbers.  */
  599.  
  600. void
  601. print_number_range (sepchar, file, a, b)
  602.      char sepchar;
  603.      struct file_data *file;
  604.      int a, b;
  605. {
  606.   int trans_a, trans_b;
  607.   translate_range (file, a, b, &trans_a, &trans_b);
  608.  
  609.   /* Note: we can have B < A in the case of a range of no lines.
  610.      In this case, we should print the line number before the range,
  611.      which is B.  */
  612.   if (trans_b > trans_a)
  613.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  614.   else
  615.     fprintf (outfile, "%d", trans_b);
  616. }
  617.  
  618. /* Look at a hunk of edit script and report the range of lines in each file
  619.    that it applies to.  HUNK is the start of the hunk, which is a chain
  620.    of `struct change'.  The first and last line numbers of file 0 are stored in
  621.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. 
  622.    Note that these are internal line numbers that count from 0.
  623.  
  624.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  625.  
  626.    Also set *DELETES nonzero if any lines of file 0 are deleted
  627.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  628.    If only ignorable lines are inserted or deleted, both are
  629.    set to 0.  */
  630.  
  631. void
  632. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  633.      struct change *hunk;
  634.      int *first0, *last0, *first1, *last1;
  635.      int *deletes, *inserts;
  636. {
  637.   int f0, l0, f1, l1, show_from, show_to;
  638.   int i;
  639.   int nontrivial = !(ignore_blank_lines_flag || ignore_regexp_list);
  640.   struct change *next;
  641.  
  642.   show_from = show_to = 0;
  643.  
  644.   f0 = hunk->line0;
  645.   f1 = hunk->line1;
  646.  
  647.   for (next = hunk; next; next = next->link)
  648.     {
  649.       l0 = next->line0 + next->deleted - 1;
  650.       l1 = next->line1 + next->inserted - 1;
  651.       show_from += next->deleted;
  652.       show_to += next->inserted;
  653.  
  654.       for (i = next->line0; i <= l0 && ! nontrivial; i++)
  655.     if (!ignore_blank_lines_flag || files[0].linbuf[i][0] != '\n')
  656.       {
  657.         struct regexp_list *r;
  658.         const char *line = files[0].linbuf[i];
  659.         int len = files[0].linbuf[i + 1] - line;
  660.  
  661.         for (r = ignore_regexp_list; r; r = r->next)
  662.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  663.         break;    /* Found a match.  Ignore this line.  */
  664.         /* If we got all the way through the regexp list without
  665.            finding a match, then it's nontrivial.  */
  666.         if (r == NULL)
  667.           nontrivial = 1;
  668.       }
  669.  
  670.       for (i = next->line1; i <= l1 && ! nontrivial; i++)
  671.     if (!ignore_blank_lines_flag || files[1].linbuf[i][0] != '\n')
  672.       {
  673.         struct regexp_list *r;
  674.         const char *line = files[1].linbuf[i];
  675.         int len = files[1].linbuf[i + 1] - line;
  676.  
  677.         for (r = ignore_regexp_list; r; r = r->next)
  678.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  679.         break;    /* Found a match.  Ignore this line.  */
  680.         /* If we got all the way through the regexp list without
  681.            finding a match, then it's nontrivial.  */
  682.         if (r == NULL)
  683.           nontrivial = 1;
  684.       }
  685.     }
  686.  
  687.   *first0 = f0;
  688.   *last0 = l0;
  689.   *first1 = f1;
  690.   *last1 = l1;
  691.  
  692.   /* If all inserted or deleted lines are ignorable,
  693.      tell the caller to ignore this hunk.  */
  694.  
  695.   if (!nontrivial)
  696.     show_from = show_to = 0;
  697.  
  698.   *deletes = show_from;
  699.   *inserts = show_to;
  700. }
  701.  
  702. /* malloc a block of memory, with fatal error message if we can't do it. */
  703.  
  704. VOID *
  705. xmalloc (size)
  706.      unsigned size;
  707. {
  708.   register VOID *value;
  709.  
  710.   if (size == 0)
  711.     size = 1;
  712.  
  713.   value = (VOID *) malloc (size);
  714.  
  715.   if (!value)
  716.     fatal ("virtual memory exhausted");
  717.   return value;
  718. }
  719.  
  720. /* realloc a block of memory, with fatal error message if we can't do it. */
  721.  
  722. VOID *
  723. xrealloc (old, size)
  724.      VOID *old;
  725.      unsigned int size;
  726. {
  727.   register VOID *value;
  728.  
  729.   if (size == 0)
  730.     size = 1;
  731.  
  732.   value = (VOID *) realloc (old, size);
  733.  
  734.   if (!value)
  735.     fatal ("virtual memory exhausted");
  736.   return value;
  737. }
  738.  
  739. /* Concatenate three strings, returning a newly malloc'd string.  */
  740.  
  741. char *
  742. concat (s1, s2, s3)
  743.      char *s1, *s2, *s3;
  744. {
  745.   int len = strlen (s1) + strlen (s2) + strlen (s3);
  746.   char *new = (char *) xmalloc (len + 1);
  747.   strcpy (new, s1);
  748.   strcat (new, s2);
  749.   strcat (new, s3);
  750.   return new;
  751. }
  752.  
  753. void
  754. debug_script (sp)
  755.      struct change *sp;
  756. {
  757.   fflush (stdout);
  758.   for (; sp; sp = sp->link)
  759.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  760.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  761.   fflush (stderr);
  762. }
  763.  
  764. #if !HAVE_MEMCHR
  765. char *
  766. memchr (s, c, n)
  767.      char *s;
  768.      int c;
  769.      size_t n;
  770. {
  771.   unsigned char *p = (unsigned char *) s, *lim = p + n;
  772.   for (;  p < lim;  p++)
  773.     if (*p == c)
  774.       return (char *) p;
  775.   return 0;
  776. }
  777. #endif
  778.